<?php
namespace app\customer\controller;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Course extends Base{
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
    public function course_list(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('course')->alias('a')->join('tcate b', 'a.cate_id = b.id','LEFT')->field('a.*,b.catename')->where('a.cus_id',session('customer')['id'])->where('a.time','>',"{$fromtime}")->where('a.time','<',"{$totime}")->where('a.title','like',"%{$key}%")->where('a.state','<',3)->order('id desc')->paginate(10);
        $lists=Db::name('course')->alias('a')->join('tcate b', 'a.cate_id = b.id','LEFT')->field('a.*,b.catename')->where('a.cus_id',session('customer')['id'])->where('a.time','>',"{$fromtime}")->where('a.time','<',"{$totime}")->where('a.title','like',"%{$key}%")->where('a.state','<',3)->select();
        $data=$list->all();
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function teachers(){
        $teachers=Db::name('teacher')->where('cate_id',$_GET["tcate_id"])->where('cus_id',session('customer')['id'])->where('state',1)->order('sort desc')->select();
        if($teachers){
            return json(['code' => 200,'data' =>$teachers,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '失败']);
        }
    }
    public function course_add(){
        if(request()->isGet()){
            $tcates=Db::name('tcate')->where('type',1)->where('cus_id',session('customer')['id'])->where('state',1)->order('sort desc')->select();
            $ccates=Db::name('tcate')->where('type',2)->where('cus_id',session('customer')['id'])->where('state',1)->order('sort desc')->select();
            $this->assign('tcates',$tcates);
            $this->assign('ccates',$ccates);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['cate_id']=$param['cate_id'];
            $data['tcate_id']=$param['tcate_id'];
            $data['teacher_id']=$param['teach_id'];
            $data['title']=$param['title'];
            $data['intro']=$param['intro'];
            $data['contentc']=html_entity_decode($param['contentc']);
            // $data['contentt']=html_entity_decode($param['contentt']);
            $data['img']=$param['img'];
            $data['sort']=$param['sort'];
            $data['flag']=1;
            $data['state']=1;
            $data['time']=time();
            $data['uptime']=time();
            $res=Db::name('course')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function course_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('course')->where('id',$param['id'])->find();
            $tcates=Db::name('tcate')->where('type',1)->where('cus_id',session('customer')['id'])->where('state',1)->order('sort desc')->select();
            $ccates=Db::name('tcate')->where('type',2)->where('cus_id',session('customer')['id'])->where('state',1)->order('sort desc')->select();
            $teachers=Db::name('teacher')->alias('a')->join('course b', 'b.tcate_id = a.cate_id','LEFT')->field('a.*')->where('a.cus_id',session('customer')['id'])->where('b.id',$param['id'])->where('a.state',1)->order('a.sort desc')->select();
            $this->assign('tcates',$tcates);
            $this->assign('ccates',$ccates);
            $this->assign('teachers',$teachers);
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cate_id']=$param['cate_id'];
            $data['tcate_id']=$param['tcate_id'];
            $data['teacher_id']=$param['teach_id'];
            $data['title']=$param['title'];
            $data['intro']=$param['intro'];
            $data['contentc']=html_entity_decode($param['contentc']);
            // $data['contentt']=html_entity_decode($param['contentt']);
            $data['img']=$param['img'];
            $data['sort']=$param['sort'];
            $data['flag']=1;
            $data['state']=1;
            $data['uptime']=time();
            $res= Db::name('course')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('course')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('course')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function flag_off(){
        $id=input('param.id');
        $status = Db::name('course')->where(array('id'=>$id))->update(['flag'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function flag_on(){
        $id=input('param.id');
        $status = Db::name('course')->where(array('id'=>$id))->update(['flag'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function course_del(){
        $param = input('post.');
        $res= Db::name('course')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function course_dels(){
        $param = input('post.');
        $res= Db::name('course')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
}
